//
//  HHomeViewController.m
//  SCM
//
//  Created by huangzhen on 2017/2/7.
//  Copyright © 2017年 huangzhen. All rights reserved.
//

#import "HHomeViewController.h"
#import <AFNetworking.h>
#import <FMDB.h>
#import "FMDBModel.h"
#import "Networking.h"
#import <TMCache.h>
#import "HOwner.h"
#import "UserDefault.h"
#import "HSCDViewController.h"
#import "HHomeCollectionViewCell.h"
#import "HDepartViewController.h"
#import "HQualityViewController.h"
#import "HSatisViewController.h"
#import "HNightQualiatyViewController.h"
#import "HSeeRecordViewController.h"
#import "HNurseAssessViewController.h"
#import "HTestViewController.h"
#import "MyExamViewController.h"
#import "HExamPaperViewController.h"
@interface HHomeViewController ()
<UICollectionViewDelegate,
UICollectionViewDataSource>
@property (nonatomic,strong)FMDatabase *db;
@property (nonatomic,strong)NSMutableArray *letterArray;
@property (nonatomic,strong)NSMutableArray *modelArray;
@property (nonatomic,retain)UICollectionView *collectionView;


@end

@implementation HHomeViewController
- (instancetype)init {
    if (self) {
        self.letterArray = [NSMutableArray new];
        self.modelArray = [NSMutableArray new];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.barTintColor = kNavBlueColor;
    self.navigationController.navigationBar.titleTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor whiteColor]};
    [self getWorkReloadata];
    [self.view addSubview:self.collectionView];
    self.view.backgroundColor = [UIColor whiteColor];
    NSInteger dis = 3; //前后的天数
    
    NSDate*nowDate = [NSDate date];
    NSDate* theDate;
    
    if(dis!=0)
    {
        NSTimeInterval  oneDay = 24*60*60*1;  //1天的长度
        
        theDate = [nowDate initWithTimeIntervalSinceNow: +oneDay*dis ];
        //or
        theDate = [nowDate initWithTimeIntervalSinceNow: -oneDay*dis ];
    }
    else
    {
        theDate = nowDate;
    }
    
    //////////////////////////////////////////////////////////////
    theDate = [nowDate initWithTimeIntervalSinceNow: 7*dis];
    NSLog(@"====%@",theDate);
        
    
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        //创建集合视图布局对象
        UICollectionViewFlowLayout *flowLayOut = [[UICollectionViewFlowLayout alloc]init];
        //设置单元格的大小，不但有高度还有宽度，所以是size
        flowLayOut.itemSize = CGSizeMake(ScreenWidth/3 - 10 , ScreenWidth/3 - 10);
        //设置最小的左右间距
        flowLayOut.minimumInteritemSpacing = 5;
        //最小的行间距
        flowLayOut.minimumLineSpacing = 5;
        //设置滑动方向
        flowLayOut.scrollDirection = UICollectionViewScrollDirectionVertical;
        //设置边界范围
        flowLayOut.sectionInset = UIEdgeInsetsMake(10,10,10,10);
          
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, NavBarHeight, ScreenWidth, ScreenHeight) collectionViewLayout:flowLayOut];
        //给集合视图设置背景色
        _collectionView.backgroundColor = [UIColor whiteColor];
        
        //设置代理对象
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        
        _collectionView.alwaysBounceVertical = YES;
        
        [_collectionView registerClass:[HHomeCollectionViewCell class] forCellWithReuseIdentifier:@"cellid"];
        
        
    }
    return _collectionView;
}

#pragma mark -- 单元格的数量
- (NSInteger )collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.letterArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HHomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellid" forIndexPath:indexPath];
    NSDictionary *menu = self.letterArray[indexPath.row];
    cell.MenuName.text = [menu objectForKey:@"NAME"];
    cell.MenuName.lineBreakMode = NSLineBreakByWordWrapping;
    cell.MenuName.numberOfLines = 0;
    CGSize maximumLabelSize = CGSizeMake(cell.contentView.width, 9999);//labelsize的最大值
    
    //关键语句
    
    CGSize expectSize = [cell.MenuName sizeThatFits:maximumLabelSize];
    
    
    cell.MenuName.frame = CGRectMake(0, cell.MenuIcon.bottom + 5,cell.contentView.width, expectSize.height);
    
    
    if (![[menu objectForKey:@"ID"] isEqual:[NSNull null]]) {
        
        cell.MenuName.tag = [[menu objectForKey:@"ID"]integerValue];

    }
    // id name  http://thothinfo_nursing_app.mydoc.io/?t=192705
    switch (cell.MenuName.tag) {
        case 10001:
             //@"个人排班"
             cell.MenuIcon.image = [UIImage imageNamed:@"功能_个人排班"];
            break;
        case 10002:
            //@"科室排班"
            cell.MenuIcon.image = [UIImage imageNamed:@"签到"];
            break;
        case 10003:
            //@"质量检查"
            cell.MenuIcon.image = [UIImage imageNamed:@"icon-xinwen"];
            break;
        case 10004:
            //@"满意度调查"
            cell.MenuIcon.image = [UIImage imageNamed:@"icon_wendangzhongxin"];
            break;
        case 10005:
            //@"夜查房"
            cell.MenuIcon.image = [UIImage imageNamed:@"icon_gongzuoliu"];
            break;
        case 10006:
            //@"操作考核"
            cell.MenuIcon.image = [UIImage imageNamed:@"icon_gongzuoliu"];
            break;
        case 10008:
            cell.MenuIcon.image = [UIImage imageNamed:@"icon_zxks"];
            break;
        case 10007:
            cell.MenuIcon.image = [UIImage imageNamed:@"icon_ktlx"];
            break;
        default:
            break;
    }
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary *menu = self.letterArray[indexPath.row];
    NSInteger menuid = [[menu objectForKey:@"ID"]integerValue];
    NSString *string=  [menu objectForKey:@"PARAM"];
    NSString *stringUP = @"";
    if (![string isEqual:[NSNull null]]) {
        stringUP = [string uppercaseString];
    }else {
        stringUP = string;
    }
    NSDictionary *dic = [super getMenuParam:stringUP];
    if (menuid == 10001)
    {
        HSCDViewController *HSCDVC = [[HSCDViewController alloc]init];
        HSCDVC.hidesBottomBarWhenPushed = YES;
        HSCDVC.title = @"我的排班";
        [self.navigationController pushViewController:HSCDVC animated:YES];
        
    }
    else if (menuid == 10002)
    {
        HDepartViewController *HDepartVC = [[HDepartViewController alloc]init];
        HDepartVC.title = @"科室排班";
        HDepartVC.hidesBottomBarWhenPushed = YES;
        HDepartVC.menuDic = dic;
        [self.navigationController pushViewController:HDepartVC animated:YES];
        
    }
    else if (menuid == 10003)
    {
       
        if (![dic.allKeys containsObject:@"LEVEL_ID"]) {
            [HCommon popupErrorMessage:@"未配置参数:LEVEL_ID"];
            return;
        }
        else if (![dic.allKeys containsObject:@"TYPE_ID"]){
            [HCommon popupErrorMessage:@"未配置参数:TYPE_ID"];
            return;
        }
        NSString *CODE = @"";
        if ([dic.allKeys containsObject:@"CODE"]) {
            CODE = [dic objectForKey:@"CODE"];
        }
        NSString *LEVEL_ID = [dic objectForKey:@"LEVEL_ID"];
        if (LEVEL_ID.length == 0) {
            [HCommon popupErrorMessage:@"配置的参数:LEVEL_ID值为空"];
            return;
        }
        HQualityViewController *HQVC = [[HQualityViewController alloc]initWithMemuParam:dic menu_id:menuid];
        HQVC.CODE = CODE;
        HQVC.title = @"质量检查";
        HQVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:HQVC animated:YES];
        
    }
    else if (menuid == 10004)
    {
        NSString *STEP_ID = [dic objectForKey:@"STEP_ID"];
        if (STEP_ID.length == 0) {
            [HCommon popupErrorMessage:@"配置的参数:STEP_ID为空"];
            return;
        }

        HSatisViewController *HSatis = [[HSatisViewController alloc]initWithParam:dic];
        HSatis.title = @"满意度调查";
        HSatis.hidesBottomBarWhenPushed = YES;
        
        [self.navigationController pushViewController:HSatis animated:YES];
        
    }
    else if (menuid == 10005)
    {
        if (![dic.allKeys containsObject:@"LEVEL_ID"]) {
            [HCommon popupErrorMessage:@"未配置参数:LEVEL_ID"];
            return;
        }else if (![dic.allKeys containsObject:@"TYPE_ID"])
        {
            [HCommon popupErrorMessage:@"未配置参数:TYPE_ID"];
            return;

        }else if (![[dic allKeys] containsObject:@"STANDARD_ID"])
        {
            [HCommon popupErrorMessage:@"未配置参数:STANDARD_ID"];
            return;
        }
        HSeeRecordViewController *HNightVC = [[HSeeRecordViewController alloc]initWithLevel_ID:[dic objectForKey:@"LEVEL_ID"] param:dic isHnight:1 menu_id:menuid] ;
        HNightVC.title = @"夜查房";
        HNightVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:HNightVC animated:YES];
    }
    else if(menuid == 10006){
        
        if (![dic.allKeys containsObject:@"TYPE_ID"])
        {
            [HCommon popupErrorMessage:@"未配置参数:TYPE_ID"];
            return;
        }
        
        
        NSDictionary *dictParam = [super getMenuParam:string];
        
        HNurseAssessViewController *nurseAssessVc = [[HNurseAssessViewController alloc] initWithMenuParam:dictParam];
        nurseAssessVc.title = @"操作过程考核";
        nurseAssessVc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:nurseAssessVc animated:YES];
    }
    else if (menuid == 10007) {
        MyExamViewController *HTestVC = [[MyExamViewController alloc]init];
        HTestVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:HTestVC animated:YES];
    }
    else if (menuid == 10008) {
        HExamPaperViewController *HExamPaper = [[HExamPaperViewController alloc]init];
        HExamPaper.hidesBottomBarWhenPushed = YES;
        HExamPaper.title = @"在线考试";
        [self.navigationController pushViewController:HExamPaper animated:YES];
    }

}
- (void)getWorkReloadata {
    
    self.letterArray = [[TMCache sharedCache]objectForKey:@"menu"];

    [self configureDataWithDictionary];
    
}
- (void)configureDataWithDictionary
{

    [self.collectionView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
